// pages/modify/modify.js
var app = getApp();
var util = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
	data: {
		goods_image:'/images/updat.png'
	},

    /**
     * 生命周期函数--监听页面加载
     */
	onLoad: function (options) {
		var that = this;
		util.showLoading();
		that.setData({
			goods_id: options.goods_id,
		});
		util.fetchPost("/api/store/store/goods_detail", {
			goods_id: that.data.goods_id
		}, function (res) {
			if (res.state == 1) {
				that.setData({
					goods_info: res.datas,
					goods_image:res.datas.image,
				})
				that.getClassList();
				util.hideLoading();
			} else {
				util.hideLoading();
				util.message(res.datas)
			}
		})
	},

	bindPickerChange:function(e){
		this.setData({
			index:e.detail.value,
		});
	
	},
	switch1Change: function (e) {
		var that = this;
		if (e.detail.value == true) {
			that.data.goods_info.if_bao=1
			
		} else {
			that.data.goods_info.if_bao = 2
		}
		that.setData({
			goods_info: that.data.goods_info
		})
	},
    getClassList:function(){
        var that = this;
        util.showLoading();
        util.fetchPost("/api/store/store/goods_class_list", {
        }, function (res) {
			console.log(res)
            if (res.state == 1) {
                var array = [];
                var index = 0;
				var arratid=""
                for (var i in res.datas) {
					array.push(res.datas[i].name)
                    if(res.datas[i].id == that.data.goods_info.gc_id){
						index = i;
                    }
                }
				
                that.setData({
                    array:array,
					index:index,
                    classlist: res.datas,
                })
				
				
            } else {
                util.message(res.datas)
            }
        })
        util.hideLoading();
    },
    delTap:function(){
    	var that = this;
    	util.confirm('您确定删除该商品吗？删除后不可恢复',function(res){
    		if(res.confirm){
    			util.showLoading();
                util.fetchPost("/api/store/store/del_goods", {
                    goods_id:that.data.goods_id
                }, function (res) {
                    if (res.state == 1) {
                        util.showSuccess(res.datas);
                        setTimeout(function(){
                            wx.navigateBack();
                        },1000);
                    } else {
                        util.message(res.datas)
                    }
                })
                util.hideLoading();
    		}
    	});
    },
	uploadImage: function () {
		var that = this;
		wx.chooseImage({
			success(res) {
				util.showLoading("图片上传中");
				const tempFilePaths = res.tempFilePaths
				wx.uploadFile({
					url: app.globalData.http_url + '/api/v1/common/upload', //仅为示例，非真实的接口地址
					header: {
						'Content-Type': 'multipart/form-data'
					},
					filePath: tempFilePaths[0],
					name: 'file',
					formData: {
						key: app.globalData.key
					},
					success: function (result) {
						util.hideLoading();
						var image_all = that.data.image_all || [];
						//上传图片后返回的是字符串，需json.prise一下
						var res = JSON.parse(result.data);
						that.setData({
							goods_image: res.path,
							image: res.id,
						});
					},
					fail: function (res) {
						utils.hideLoading();
						utils.message('上传服务器上传失败,请稍后再试2');
						console.log(res);
					}
				})
			}
		})
	},
	formSubmit: function (e) {
		var that = this;
		console.log(that.data.classlist[that.data.index].id)
		var name = e.detail.value.name;
		var sort = e.detail.value.sort;
		var price = e.detail.value.price;
		var stock = e.detail.value.stock;
		if (name.length < 1) {
			util.showSuccess('请输入商品名称', 'none', false, 800);
			return false;
		}
		var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
		var money = "520.100";
		if (!reg.test(price)) {
			util.showSuccess('请填写正确的商品价格', 'none', false, 800);
			return false;
		};
		if (sort.length < 1) {
			util.showSuccess('请输入排序', 'none', false, 800);
			return false;
		}
		if (stock.length <= 0) {
			util.showSuccess('请输入库存', 'none', false, 800);
			return false;
		}
		if (stock == 0) {
			util.showSuccess('库存不能小于0', 'none', false, 800);
			return false;
		}
		if (that.data.goods_image == '/images/updat.png') {
			util.showSuccess('请上传商品图片', 'none', false, 800);
			return false;
		}
		util.showLoading();
		var apiUrl = '/api/store/store/add_modify_goods_v3';
		util.fetchPost(apiUrl, {
			goods_id: that.data.goods_info.id,
			gc_id: that.data.classlist[that.data.index].id,
			name: name,
			price: price,
			image: that.data.goods_image,
			sort: sort,
			stock:stock,
			if_bao: that.data.goods_info.if_bao
		}, function (res) {
			util.hideLoading();
			if (res.state == 1) {
				util.showSuccess('操作成功');
				setTimeout(function () {
					wx.navigateBack();
				}, 2000);
			} else {
				util.message(res.datas);
			}
		})
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
	onReady: function () {

	},

    /**
     * 生命周期函数--监听页面显示
     */
	onShow: function () {

	},

    /**
     * 生命周期函数--监听页面隐藏
     */
	onHide: function () {

	},

    /**
     * 生命周期函数--监听页面卸载
     */
	onUnload: function () {

	},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
	onPullDownRefresh: function () {

	},

    /**
     * 页面上拉触底事件的处理函数
     */
	onReachBottom: function () {

	},

    /**
     * 用户点击右上角分享
     */
	onShareAppMessage: function () {

	}
})